home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d892.lha / Indent / source / source.lha / io.c < prev    next >
C/C++ Source or Header  |  1993-06-24  |  21KB  |  922 lines

  1. /* Copyright (c) 1992, Free Software Foundation, Inc.  All rights reserved.
  2.  
  3.    Copyright (c) 1985 Sun Microsystems, Inc. Copyright (c) 1980 The Regents
  4.    of the University of California. Copyright (c) 1976 Board of Trustees of
  5.    the University of Illinois. All rights reserved.
  6.  
  7.    Redistribution and use in source and binary forms are permitted
  8.    provided that
  9.    the above copyright notice and this paragraph are duplicated in all such
  10.    forms and that any documentation, advertising materials, and other
  11.    materials related to such distribution and use acknowledge that the
  12.    software was developed by the University of California, Berkeley, the
  13.    University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  14.    either University or Sun Microsystems may not be used to endorse or
  15.    promote products derived from this software without specific prior written
  16.    permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  18.    OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
  19.  
  20.  
  21. #include "sys.h"
  22. #include "indent.h"
  23. #include <ctype.h>
  24.  
  25. #ifdef VMS
  26. #   include <file.h>
  27. #   include <types.h>
  28. #   include <stat.h>
  29. #else  /* not VMS */
  30.  
  31. /* POSIX says that <fcntl.h> should exist.  Some systems might need to use
  32.    <sys/fcntl.h> or <sys/file.h> instead.  */
  33. #include <fcntl.h>
  34.  
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #endif /* not VMS */
  38.  
  39. /* number of levels a label is placed to left of code */
  40. #define LABEL_OFFSET 2
  41.  
  42.  
  43. /* Stuff that needs to be shared with the rest of indent. Documented in
  44.    indent.h.  */
  45. char *in_prog;
  46. char *in_prog_pos;
  47. char *cur_line;
  48. unsigned long in_prog_size;
  49. FILE *output;
  50. char *buf_ptr;
  51. char *buf_end;
  52. int had_eof;
  53. int out_lines;
  54. int com_lines;
  55.  
  56. int suppress_blanklines = 0;
  57. static int comment_open;
  58.  
  59. int paren_target;
  60.  
  61. /* Use `perror' to print the system error message
  62.    caused by OFFENDER. */
  63.  
  64. static char *errbuf;
  65.  
  66. void
  67. sys_error (offender)
  68.      char *offender;
  69. {
  70.   int size = strlen (offender);
  71.   static int buffer_size;
  72.  
  73.   if (errbuf == 0)
  74.     {
  75.       buffer_size = size + 10;    /* Extra for random unix lossage */
  76.       errbuf = (char *) xmalloc (buffer_size);
  77.     }
  78.   else if (size + 10 > buffer_size)
  79.     {
  80.       buffer_size = size + 10;
  81.       errbuf = xrealloc (errbuf, buffer_size);
  82.     }
  83.   sprintf (errbuf, "indent: %s", offender);
  84.   perror (errbuf);
  85.   exit (1);
  86. }
  87.  
  88. #ifdef VMS
  89. int
  90. vms_read (int file_desc, void *buffer, int nbytes)
  91. {
  92.     register char *bufp;
  93.     register int nread, nleft;
  94.  
  95.     bufp  = buffer;
  96.     nread = 0;
  97.     nleft = nbytes;
  98.  
  99.     while (nread = read (file_desc, bufp, nleft), nread > 0)
  100.       {
  101.         bufp += nread;
  102.         nleft -= nread;
  103.         if (nleft < 0)
  104.             sys_error ("Internal buffering error");
  105.     }
  106.  
  107.     return nread;
  108. }
  109. #endif /* VMS */
  110.  
  111. INLINE int
  112. count_columns (column, bp)
  113.      int column;
  114.      char *bp;
  115. {
  116.   while (*bp != '\0')
  117.     {
  118.       switch (*bp++)
  119.     {
  120.     case EOL:
  121.     case 014:        /* form feed */
  122.       column = 1;
  123.       break;
  124.     case TAB:
  125.       column += tabsize - (column - 1) % tabsize;
  126.       break;
  127.     case 010:        /* backspace */
  128.       --column;
  129.       break;
  130.     default:
  131.       ++column;
  132.       break;
  133.     }
  134.     }
  135.  
  136.   return column;
  137. }
  138.  
  139. /* Return the column we are at in the input line. */
  140.  
  141. INLINE int
  142. current_column ()
  143. {
  144.   char *p;
  145.   int column = 1;
  146.  
  147.   if (buf_ptr >= save_com.ptr && buf_ptr <= save_com.end)
  148.     p = save_com.ptr;
  149.   else
  150.     p = cur_line;
  151.  
  152. #if 0
  153.   /* Paranoia */
  154.   if (! (buf_ptr >= cur_line && buf_ptr < in_prog_pos))
  155.     abort ();
  156. #endif
  157.  
  158.   column = 1;
  159.   while (p < buf_ptr)
  160.     switch (*p++)
  161.       {
  162.       case EOL:
  163.       case 014:            /* form feed */
  164.     column = 1;
  165.     break;
  166.       case TAB:
  167.     column += tabsize - (column - 1) % tabsize;
  168. #if 0
  169.     column += tabsize - (column % tabsize) + 1;
  170. #endif
  171.     break;
  172.       case '\b':        /* backspace */
  173.     column--;
  174.     break;
  175.       default:
  176.     column++;
  177.     break;
  178.       }
  179.  
  180.   return column;
  181. }
  182.  
  183. void
  184. dump_line ()
  185. {                /* dump_line is the routine that actually
  186.                    effects the printing of the new source. It
  187.                    prints the label section, followed by the
  188.                    code section with the appropriate nesting
  189.                    level, followed by any comments */
  190.   register int cur_col;
  191.   register int target_col = 0;
  192.   static not_first_line;
  193.  
  194.   if (parser_state_tos->procname[0])
  195.     {
  196.       if (troff)
  197.     {
  198.       if (comment_open)
  199.         {
  200.           comment_open = 0;
  201.           fprintf (output, ".*/\n");
  202.         }
  203.       fprintf (output, ".Pr \"%.*s\"\n",
  204.            parser_state_tos->procname_end - parser_state_tos->procname,
  205.            parser_state_tos->procname);
  206.     }
  207.       parser_state_tos->ind_level = 0;
  208.       parser_state_tos->procname = "\0";
  209.     }
  210.  
  211.   /* A blank line */
  212.   if (s_code == e_code && s_lab == e_lab && s_com == e_com)
  213.     {
  214.       /* If we have a formfeed on a blank line, we should just output it,
  215.          rather than treat it as a normal blank line.  */
  216.       if (parser_state_tos->use_ff)
  217.     {
  218.       putc ('\014', output);
  219.       parser_state_tos->use_ff = false;
  220.     }
  221.       else
  222.     {
  223.       if (suppress_blanklines > 0)
  224.         suppress_blanklines--;
  225.       else
  226.         {
  227.           parser_state_tos->bl_line = true;
  228.           n_real_blanklines++;
  229.         }
  230.     }
  231.     }
  232.   else
  233.     {
  234.       suppress_blanklines = 0;
  235.       parser_state_tos->bl_line = false;
  236.       if (prefix_blankline_requested
  237.       && not_first_line
  238.       && n_real_blanklines == 0)
  239.     n_real_blanklines = 1;
  240.       else if (swallow_optional_blanklines && n_real_blanklines > 1)
  241.     n_real_blanklines = 1;
  242.  
  243.       while (--n_real_blanklines >= 0)
  244.     putc (EOL, output);
  245.       n_real_blanklines = 0;
  246.       if (parser_state_tos->ind_level == 0)
  247.     parser_state_tos->ind_stmt = 0;    /* this is a class A kludge. dont do
  248.                        additional statement indentation
  249.                        if we are at bracket level 0 */
  250.  
  251.       if (e_lab != s_lab || e_code != s_code)
  252.     ++code_lines;        /* keep count of lines with code */
  253.  
  254.  
  255.       if (e_lab != s_lab)
  256.     {            /* print lab, if any */
  257.       if (comment_open)
  258.         {
  259.           comment_open = 0;
  260.           fprintf (output, ".*/\n");
  261.         }
  262.       while (e_lab > s_lab && (e_lab[-1] == ' ' || e_lab[-1] == TAB))
  263.         e_lab--;
  264.       cur_col = pad_output (1, compute_label_target ());
  265.       if (s_lab[0] == '#' && (strncmp (s_lab, "#else", 5) == 0
  266.                   || strncmp (s_lab, "#endif", 6) == 0))
  267.         {
  268.           /* Treat #else and #endif as a special case because any text
  269.              after #else or #endif should be converted to a comment.  */
  270.           register char *s = s_lab;
  271.           if (e_lab[-1] == EOL)
  272.         e_lab--;
  273.           do
  274. #ifdef AMIGA
  275.         fputc (*s++, output);
  276. #else /* !AMIGA */
  277.         putc (*s++, output);
  278. #endif /* !AMIGA */
  279.           while (s < e_lab && 'a' <= *s && *s <= 'z');
  280.           while ((*s == ' ' || *s == TAB) && s < e_lab)
  281.         s++;
  282.           if (s < e_lab)
  283.         {
  284.           if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
  285.             fprintf (output, (tabsize > 1 ? "\t%.*s" : "  %.*s"),
  286.                  e_lab - s, s);
  287.           else
  288.             fprintf (output, (tabsize > 1
  289.                       ? "\t/* %.*s */"
  290.                       : "  /* %.*s */"),
  291.                  e_lab - s, s);
  292.         }
  293.         }
  294.       else
  295.         fprintf (output, "%.*s", e_lab - s_lab, s_lab);
  296.       cur_col = count_columns (cur_col, s_lab);
  297.     }
  298.       else
  299.     cur_col = 1;        /* there is no label section */
  300.  
  301.       parser_state_tos->pcase = false;
  302.  
  303.       if (s_code != e_code)
  304.     {            /* print code section, if any */
  305.       register char *p;
  306.       register i;
  307.  
  308.       if (comment_open)
  309.         {
  310.           comment_open = 0;
  311.           fprintf (output, ".*/\n");
  312.         }
  313.  
  314.       /* If a comment begins this line, then indent it to the right
  315.          column for comments, otherwise the line starts with code,
  316.          so indent it for code. */
  317.       if (embedded_comment_on_line == 1)
  318.         target_col = parser_state_tos->com_col;
  319.       else
  320.         target_col = compute_code_target ();
  321.  
  322.       /* If a line ends in an lparen character, the following line should
  323.          not line up with the parenthesis, but should be indented by the
  324.          usual amount.  */
  325.       if (parser_state_tos->last_token == lparen)
  326.         {
  327.           parser_state_tos->paren_indents[parser_state_tos->p_l_follow - 1]
  328.         += ind_size - 1;
  329.         }
  330.  
  331.       for (i = 0; i < parser_state_tos->p_l_follow; i++)
  332.         if (parser_state_tos->paren_indents[i] >= 0)
  333.           parser_state_tos->paren_indents[i]
  334.         = -(parser_state_tos->paren_indents[i] + target_col);
  335.  
  336.       cur_col = pad_output (cur_col, target_col);
  337.       for (p = s_code; p < e_code; p++)
  338.         {
  339. #if 0
  340.           if (*p == (char) 0200)
  341.         fprintf (output, "%d", (int) (target_col * 7));
  342.           else
  343.         if (tabsize > 1)
  344. #endif
  345.         putc (*p, output);
  346.  
  347. #if 0
  348.           else
  349.         {
  350.           int width = 1;
  351.           if (*p == TAB)
  352.             width = (tabsize - ((cur_col - 1) % tabsize));
  353.           cur_col += width;
  354.           while (width--)
  355.             putc (*p, output);
  356.         }
  357. #endif
  358.         }
  359.       cur_col = count_columns (cur_col, s_code);
  360.     }
  361.  
  362.       if (s_com != e_com)
  363.     {
  364.       if (troff)
  365.         {
  366.           int all_here = 0;
  367.           register char *p;
  368.  
  369.           if (e_com[-1] == '/' && e_com[-2] == '*')
  370.         e_com -= 2, all_here++;
  371.           while (e_com > s_com && e_com[-1] == ' ')
  372.         e_com--;
  373.           *e_com = 0;
  374.           p = s_com;
  375.           while (*p == ' ')
  376.         p++;
  377.           if (p[0] == '/' && p[1] == '*')
  378.         p += 2, all_here++;
  379.           else if (p[0] == '*')
  380.         p += p[1] == '/' ? 2 : 1;
  381.           while (*p == ' ')
  382.         p++;
  383.           if (*p == 0)
  384.         goto inhibit_newline;
  385.           if (comment_open < 2 && parser_state_tos->box_com)
  386.         {
  387.           comment_open = 0;
  388.           fprintf (output, ".*/\n");
  389.         }
  390.           if (comment_open == 0)
  391.         {
  392.           if ('a' <= *p && *p <= 'z')
  393.             *p = *p + 'A' - 'a';
  394.           if (e_com - p < 50 && all_here == 2)
  395.             {
  396.               register char *follow = p;
  397.               fprintf (output, "\n.nr C! \\w\1");
  398.               while (follow < e_com)
  399.             {
  400.               switch (*follow)
  401.                 {
  402.                 case EOL:
  403.                   putc (' ', output);
  404.                 case 1:
  405.                   break;
  406.                 case '\\':
  407.                   putc ('\\', output);
  408.                 default:
  409.                   putc (*follow, output);
  410.                 }
  411.               follow++;
  412.             }
  413.               putc (1, output);
  414.             }
  415.           fprintf (output, "\n./* %dp %d %dp\n",
  416.                (int) (parser_state_tos->com_col * 7),
  417.                (int) ((s_code != e_code || s_lab != e_lab)
  418.                   - parser_state_tos->box_com),
  419.                (int) (target_col * 7));
  420.         }
  421.           comment_open = 1 + parser_state_tos->box_com;
  422.           while (*p)
  423.         {
  424. #ifdef AMIGA
  425.           if (*p == BACKSLASH)
  426.             fputc (BACKSLASH, output);
  427.           fputc (*p++, output);
  428. #else /* !AMIGA */
  429.           if (*p == BACKSLASH)
  430.             putc (BACKSLASH, output);
  431.           putc (*p++, output);
  432. #endif /* !AMIGA */
  433.         }
  434.         }
  435.       else
  436.         {
  437.           /* Here for comment printing.  This code is new as of
  438.              version 1.8 */
  439.           register target = parser_state_tos->com_col;
  440.           register char *com_st = s_com;
  441.  
  442.           if (cur_col > target)
  443.         {
  444.           putc (EOL, output);
  445.           cur_col = 1;
  446.           ++out_lines;
  447.         }
  448.  
  449.           cur_col = pad_output (cur_col, target);
  450.           fwrite (com_st, e_com - com_st, 1, output);
  451.           cur_col += e_com - com_st;
  452.           com_lines++;
  453.         }
  454.     }
  455.       else if (embedded_comment_on_line)
  456.     com_lines++;
  457.       embedded_comment_on_line = 0;
  458.  
  459.       if (parser_state_tos->use_ff)
  460.     {
  461.       putc ('\014', output);
  462.       parser_state_tos->use_ff = false;
  463.     }
  464.       else
  465.     putc (EOL, output);
  466.  
  467.     inhibit_newline:
  468.       ++out_lines;
  469.       if (parser_state_tos->just_saw_decl == 1
  470.       && blanklines_after_declarations)
  471.     {
  472.       prefix_blankline_requested = 1;
  473.       parser_state_tos->just_saw_decl = 0;
  474.     }
  475.       else
  476.     prefix_blankline_requested = postfix_blankline_requested;
  477.       postfix_blankline_requested = 0;
  478.     }
  479.  
  480.   /* if we are in the middle of a declaration, remember that fact
  481.      for proper comment indentation */
  482.   parser_state_tos->decl_on_line = parser_state_tos->in_decl;
  483.  
  484.   /* next line should be indented if we have not completed this
  485.      stmt and if we are not in the middle of a declaration */
  486.   parser_state_tos->ind_stmt = (parser_state_tos->in_stmt
  487.                 & ~parser_state_tos->in_decl);
  488.  
  489.   parser_state_tos->dumped_decl_indent = 0;
  490.   *(e_lab  = s_lab) = '\0';    /* reset buffers */
  491.   *(e_code = s_code) = '\0';
  492.   *(e_com  = s_com) = '\0';
  493.   parser_state_tos->ind_level = parser_state_tos->i_l_follow;
  494.   parser_state_tos->paren_level = parser_state_tos->p_l_follow;
  495.   if (parser_state_tos->paren_level > 0)
  496.     paren_target
  497.       = -parser_state_tos->paren_indents[parser_state_tos->paren_level - 1];
  498.   else
  499.     paren_target = 0;
  500.   not_first_line = 1;
  501.  
  502.   return;
  503. }
  504.  
  505. /* Return the column in which we should place the code about to be output. */
  506.  
  507. INLINE int
  508. compute_code_target ()
  509. {
  510.   register target_col = parser_state_tos->ind_level + 1;
  511.   register w, t;
  512.  
  513.   if (! parser_state_tos->paren_level)
  514.     {
  515.       if (parser_state_tos->ind_stmt)
  516.     target_col += continuation_indent;
  517.       return target_col;
  518.     }
  519.  
  520.   if (!lineup_to_parens)
  521.     return target_col + (continuation_indent * parser_state_tos->paren_level);
  522.  
  523.   t = paren_target;
  524.   if ((w = count_columns (t, s_code) - max_col) > 0
  525.       && count_columns (target_col, s_code) <= max_col)
  526.     {
  527.       t -= w + 1;
  528.       if (t > target_col)
  529.     target_col = t;
  530.     }
  531.   else
  532.     target_col = t;
  533.  
  534.   return target_col;
  535. }
  536.  
  537. INLINE int
  538. compute_label_target ()
  539. {
  540.   return
  541.   parser_state_tos->pcase ? case_ind + 1
  542.   : *s_lab == '#' ? 1
  543.   : parser_state_tos->ind_level - LABEL_OFFSET + 1;
  544. }
  545.  
  546. /* VMS defines it's own read routine, `vms_read' */
  547. #ifndef SYS_READ
  548. #define SYS_READ read
  549. #endif
  550.  
  551. /* Read file FILENAME into a `fileptr' structure, and return a pointer to
  552.    that structure. */
  553.  
  554. static struct file_buffer fileptr;
  555.  
  556. struct file_buffer *
  557. read_file (filename)
  558.      char *filename;
  559. {
  560.   int fd, size;
  561.   struct stat file_stats;
  562.   int namelen = strlen (filename);
  563.  
  564.   fd = open (filename, O_RDONLY, 0777);
  565.   if (fd < 0)
  566.     sys_error (filename);
  567.  
  568.   if (fstat (fd, &file_stats) < 0)
  569.     sys_error (filename);
  570.  
  571.   if (fileptr.data != 0)
  572.     free (fileptr.data);
  573.   fileptr.size = file_stats.st_size;
  574.   fileptr.data = (char *) xmalloc (file_stats.st_size + 1);
  575.  
  576.   size = SYS_READ (fd, fileptr.data, fileptr.size);
  577.   if (size < 0)
  578.     sys_error (filename);
  579.   if (close (fd) < 0)
  580.     sys_error (filename);
  581.  
  582.   /* Apparently, the DOS stores files using CR-LF for newlines, but
  583.      then the DOS `read' changes them into '\n'.  Thus, the size of the
  584.      file on disc is larger than what is read into memory.  Thanks, Bill. */
  585.   if (size != fileptr.size)
  586.     fileptr.size = size;
  587.  
  588.   fileptr.name = (char *) xmalloc (namelen + 1);
  589.   memcpy (fileptr.name, filename, namelen);
  590.   fileptr.name[namelen] = '\0';
  591.  
  592.   fileptr.data[fileptr.size] = '\0';
  593.  
  594.   return &fileptr;
  595. }
  596.  
  597. /* This should come from stdio.h and be some system-optimal number */
  598. #ifndef BUFSIZ
  599. #define BUFSIZ 1024
  600. #endif
  601.  
  602. /* Suck the standard input into a file_buffer structure, and
  603.    return a pointer to that structure. */
  604.  
  605. struct file_buffer stdinptr;
  606.  
  607. struct file_buffer *
  608. read_stdin ()
  609. {
  610.   unsigned int size = 15 * BUFSIZ;
  611.   int ch;
  612.   register char *p;
  613.  
  614.   if (stdinptr.data != 0)
  615.     free (stdinptr.data);
  616.  
  617.   stdinptr.data = (char *) xmalloc (size + 1);
  618.   stdinptr.size = 0;
  619.   p = stdinptr.data;
  620.   do
  621.     {
  622.       while (stdinptr.size < size)
  623.     {
  624.       ch = getc (stdin);
  625.       if (ch == EOF)
  626.         break;
  627.  
  628.       *p++ = ch;
  629.       stdinptr.size++;
  630.     }
  631.  
  632.       if (ch != EOF)
  633.     {
  634.       size += (2 * BUFSIZ);
  635.       stdinptr.data = xrealloc (stdinptr.data, size);
  636.       p = stdinptr.data + stdinptr.size;
  637.     }
  638.     }
  639.   while (ch != EOF);
  640.  
  641.   stdinptr.name = "Standard Input";
  642.  
  643.   stdinptr.data[stdinptr.size] = '\0';
  644.  
  645.   return &stdinptr;
  646. }
  647.  
  648. /* Advance `buf_ptr' so that it points to the next line of input.
  649.  
  650.    If the next input line contains an indent control comment turning
  651.    off formatting (a comment, C or C++, beginning with *INDENT-OFF*),
  652.    then simply print out input lines without formatting until we find
  653.    a corresponding comment containing *INDENT-0N* which re-enables
  654.    formatting.
  655.  
  656.    Note that if this is a C comment we do not look for the closing
  657.    delimiter.  Note also that older version of this program also
  658.    skipped lines containing *INDENT** which represented errors
  659.    generated by indent in some previous formatting.  This version does
  660.    not recognize such lines. */
  661.  
  662. INLINE void
  663. fill_buffer ()
  664. {
  665.   register char *p;
  666.   int inhibit_formatting = 0;
  667.  
  668.   /* indent() may be saving the text between "if (...)" and the following
  669.      statement.  To do so, it uses another buffer (`save_com').  Switch
  670.      back to the previous buffer here. */
  671.   if (bp_save != 0)
  672.     {
  673.       buf_ptr = bp_save;
  674.       buf_end = be_save;
  675.       bp_save = be_save = 0;
  676.  
  677.       /* only return if there is really something in this buffer */
  678.       if (buf_ptr < buf_end)
  679.     return;
  680.     }
  681.  
  682.   /* If formatting gets turned off, then just loop here outputting lines
  683.      until formatting is re-enabled. */
  684.   do
  685.     {
  686.       /* Advance buf_ptr past last line, and return if EOF. */
  687.       cur_line = buf_ptr = in_prog_pos;
  688.       if (*buf_ptr == '\0')
  689.     {
  690.       had_eof = true;
  691.       return;
  692.     }
  693.  
  694.       /* Examine the beginning of the line for an indent control
  695.          comment. */
  696.       p = buf_ptr;
  697.       while (*p == ' ' || *p == TAB)
  698.     p++;
  699.       if (*p == '/' && (*(p + 1) == '*' || *(p + 1) == '/'))
  700.     {
  701.       p += 2;
  702.       while (*p == ' ' || *p == TAB)
  703.         p++;
  704.       if (! inhibit_formatting)
  705.         {
  706.           if (! strncmp (p, "*INDENT-OFF*", 12))
  707.         {
  708.           if (s_com != e_com || s_lab != e_lab || s_code != e_code)
  709.             dump_line ();
  710.           inhibit_formatting = 1;
  711.         }
  712.         }
  713.       else
  714.         {
  715.           if (! strncmp (p, "*INDENT-ON*", 11))
  716.         {
  717.           p += 11;
  718.           /* Set inhibit_formatting to 2 so that we will
  719.              still toss out this whole line, but drop out
  720.              of the loop afterwards. */
  721.           inhibit_formatting = 2;
  722.           n_real_blanklines = 0;
  723.           postfix_blankline_requested = 0;
  724.           prefix_blankline_requested = 0;
  725.           suppress_blanklines = 1;
  726.         }
  727.         }
  728.     }
  729.  
  730.       /* Now procede through the rest of the line */
  731.       while (*p != '\0' && *p != EOL)
  732.     p++;
  733.       buf_end = in_prog_pos = p + 1;
  734.  
  735.       if (inhibit_formatting)
  736.     {
  737.       p = buf_ptr;
  738.       while (p < buf_end)
  739. #ifdef AMIGA
  740.         fputc (*p++, output);
  741. #else /* !AMIGA */
  742.         putc (*p++, output);
  743. #endif /* !AMIGA */
  744.       if (inhibit_formatting == 2)
  745.         {
  746.           inhibit_formatting = 0;
  747.           continue;
  748.         }
  749.     }
  750.     }
  751.   while (inhibit_formatting);
  752. }
  753.  
  754. /* Fill the output line with whitespace up to TARGET_COLUMN, given that
  755.    the line is currently in column CURRENT_COLUMN.  Returns the ending
  756.    column. */
  757.  
  758. INLINE int
  759. pad_output (current_column, target_column)
  760.   register int current_column;
  761.   register int target_column;
  762. {
  763.   if (troff)
  764.     {
  765.       fprintf (output, "\\h'|%dp'", (int) ((target_column - 1) * 7));
  766.       return 0;
  767.     }
  768.  
  769.   if (current_column >= target_column)
  770.     return current_column;
  771.  
  772.   if (tabsize > 1)
  773.     {
  774.       register int offset;
  775.  
  776.       offset = tabsize - (current_column - 1) % tabsize;
  777.       while (current_column + offset <= target_column)
  778.     {
  779.       putc (TAB, output);
  780.       current_column += offset;
  781.       offset = tabsize;
  782.     }
  783.     }
  784.  
  785.   while (current_column < target_column)
  786.     {
  787.       putc (' ', output);
  788.       current_column++;
  789.     }
  790.  
  791.   return current_column;
  792. }
  793.  
  794. /* Nonzero if we have found an error (not a warning).  */
  795. int found_err;
  796.  
  797. /* Signal an error.  LEVEL is nonzero if it is an error (as opposed to a
  798.    warning.  MSG is a printf-style format string.  Additional arguments are
  799.    additional arguments for printf.  */
  800. /* VARARGS2 */
  801. diag (level, msg, a, b)
  802.      int level;
  803.      unsigned int a, b;
  804.      char *msg;
  805. {
  806.   if (level)
  807.     found_err = 1;
  808.  
  809.   fprintf (stderr, "indent:%s:%d: %s: ", in_name, (int) line_no,
  810.        level == 0 ? "Warning" : "Error");
  811.  
  812.   if (msg)
  813.     fprintf (stderr, msg, a, b);
  814.  
  815.   fprintf (stderr, "\n");
  816. }
  817.  
  818. writefdef (f, nm)
  819.      register struct fstate *f;
  820.      unsigned int nm;
  821. {
  822.   fprintf (output, ".ds f%c %s\n.nr s%c %d\n",
  823.        (int) nm, f->font, nm, (int) f->size);
  824. }
  825.  
  826. /* Write characters starting at S to change the font from OF to NF.  Return a
  827.    pointer to the character after the last character written. For troff mode
  828.    only.  */
  829. char *
  830. chfont (of, nf, s)
  831.      register struct fstate *of, *nf;
  832.      char *s;
  833. {
  834.   if (of->font[0] != nf->font[0]
  835.       || of->font[1] != nf->font[1])
  836.     {
  837.       *s++ = '\\';
  838.       *s++ = 'f';
  839.       if (nf->font[1])
  840.     {
  841.       *s++ = '(';
  842.       *s++ = nf->font[0];
  843.       *s++ = nf->font[1];
  844.     }
  845.       else
  846.     *s++ = nf->font[0];
  847.     }
  848.   if (nf->size != of->size)
  849.     {
  850.       *s++ = '\\';
  851.       *s++ = 's';
  852.       if (nf->size < of->size)
  853.     {
  854.       *s++ = '-';
  855.       *s++ = '0' + of->size - nf->size;
  856.     }
  857.       else
  858.     {
  859.       *s++ = '+';
  860.       *s++ = '0' + nf->size - of->size;
  861.     }
  862.     }
  863.   return s;
  864. }
  865.  
  866. void
  867. parsefont (f, s0)
  868.      register struct fstate *f;
  869.      char *s0;
  870. {
  871.   register char *s = s0;
  872.   int sizedelta = 0;
  873.   int i;
  874.  
  875.   f->size = 0;
  876.   f->allcaps = 1;
  877.   for (i = 0; i < 4; i++)
  878.     f->font[i] = 0;
  879.  
  880.   while (*s)
  881.     {
  882.       if (isdigit (*s))
  883.     f->size = f->size * 10 + *s - '0';
  884.       else if (isupper (*s))
  885.     if (f->font[0])
  886.       f->font[1] = *s;
  887.     else
  888.       f->font[0] = *s;
  889.       else if (*s == 'c')
  890.     f->allcaps = 1;
  891.       else if (*s == '+')
  892.     sizedelta++;
  893.       else if (*s == '-')
  894.     sizedelta--;
  895.       else
  896.     {
  897.       fprintf (stderr, "indent: bad font specification: %s\n", s0);
  898.       exit (1);
  899.     }
  900.       s++;
  901.     }
  902.   if (f->font[0] == 0)
  903.     f->font[0] = 'R';
  904.   if (bodyf.size == 0)
  905.     bodyf.size = 11;
  906.   if (f->size == 0)
  907.     f->size = bodyf.size + sizedelta;
  908.   else if (sizedelta > 0)
  909.     f->size += bodyf.size;
  910.   else
  911.     f->size = bodyf.size - f->size;
  912. }
  913.  
  914. #ifdef DEBUG
  915. void
  916. dump_debug_line ()
  917. {
  918.   fprintf (output, "\n*** Debug output marker line ***\n");
  919. }
  920.  
  921. #endif
  922.